package com.hs.srm.admin.modules.orders.controller;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hs.srm.admin.modules.orders.entity.DeliverEntity;
import com.hs.srm.admin.modules.orders.entity.ScmPoItemEntity;
import com.hs.srm.admin.modules.orders.service.DeliverService;
import com.hs.srm.admin.modules.sys.controller.AbstractController;
import com.hs.srm.base.common.config.SysConfig;
import com.hs.srm.base.common.utils.EntityUtils;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;




/**
 * @author yanfei
 *
 * @date 2019年4月8日
 * 发货明细
 */
@Controller
@RequestMapping("/deliver")
public class DeliverController  extends AbstractController {
	
	private static final long serialVersionUID = 5861515635235276502L;
	@Autowired
	private DeliverService deliverService;
	
		/**
		 * 查询发货明细列表
		 */
		@RequestMapping("/list")
		@ResponseBody
		public R list(@RequestParam Map<String, Object> params) {
			PageUtils pu = this.deliverService.selectDetailPage(params);
			List<DeliverEntity> voList = (List<DeliverEntity>)pu.getList();
			voList = EntityUtils.valToTxt(voList);
			return R.ok(pu);
		}
		
		@RequestMapping("/toDwnload")
		public void toDwnload(HttpServletResponse response,HttpServletRequest request) {
			 // 设置文件名，根据业务需要替换成要下载的文件名
			 String pkId = request.getParameter("id").trim();
			 DeliverEntity deliver = this.deliverService.selectById(pkId);
			 String url = deliver.getPhotoUrl();
			 String fileName = url.substring(url.lastIndexOf("\\")+1);
			if (fileName != null) {
		        //设置文件路径
				String realPath = SysConfig.getUploadPath();
				File file = new File(realPath, fileName);
				//File myFile = new File("C:" + File.separator + "demo" + File.separator, "aa.txt");
		        if (file.exists()) {
		            response.setContentType("application/force-download");// 设置强制下载不打开
		            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
		            byte[] buffer = new byte[1024];
		            FileInputStream fis = null;
		            BufferedInputStream bis = null;
		            try {
		                fis = new FileInputStream(file);
		                bis = new BufferedInputStream(fis);
		                OutputStream os = response.getOutputStream();
		                int i = bis.read(buffer);
		                while (i != -1) {
		                    os.write(buffer, 0, i);
		                    i = bis.read(buffer);
		                }
		            } catch (Exception e) {
		                e.printStackTrace();
		            } finally {
		                if (bis != null) {
		                    try {
		                        bis.close();
		                    } catch (IOException e) {
		                        e.printStackTrace();
		                    }
		                }
		                if (fis != null) {
		                    try {
		                        fis.close();
		                    } catch (IOException e) {
		                        e.printStackTrace();
		                    }
		                }
		            }
		        }
		    }
		}
}
